package spark

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
  def main(args: Array[String]): Unit = {
    //1.创建sparkcontext
    val conf = new SparkConf().setMaster("local[*]").setAppName("word_count");
    val sc = new SparkContext(conf);

    //2.加载文件,并处理
    val rdd1: RDD[String] = sc.textFile("dataSet/wordcount.txt")
    val rdd2: RDD[String] = rdd1.flatMap(item => item.split(","))
    val rdd3: RDD[(String, Int)] = rdd2.map(item => (item, 1))
    val rdd4: RDD[(String, Int)] = rdd3.reduceByKey(_+_)
    //3.得到结果
    rdd4.collect().foreach(item => println(item))
  }
}
